<?php

namespace app\admin\controller;


use think\Controller;
use think\Db;

class Set extends Base
{
    public function email()
    {
        return $this->fetch();
    }

    public function website()
    {
        return $this->fetch();
    }

    public function password1()
    {
        if (request()->isAjax()) {
            $data = input('');


            if ($data['password']) {
                $data['password'] = cmf_password($data['password']);
            }
            $res = Db::name('admin')->where('password', $data['password'])->update($data);
            if ($res === false) {
                $this->success('操作失败');
            }
            $this->success('操作成功');

        }

        return $this->fetch();
    }


    public function password()
    {

        if (request()->isAjax()) {

            $data = input('');
            $oldpassword = trim($data['oldpassword']);
            $password = trim($data['password']);
            $repassword = trim($data['repassword']);
            if (!$oldpassword || !$password || !$repassword) {
                $this->error('数据异常');
            }

            if ($password != $repassword) {
                $this->error('两次密码不一致');
            }

            $pass = Db::name('admin')->where('id', $this->admin_id)->value('password');
            if ($pass != cmf_password($oldpassword)) {
                $this->error('密码错误');
            }
            Db::name('admin')->where('id', $this->admin_id)->update(['password' => cmf_password($password)]);
            $this->success('修改成功');


        }

        return $this->fetch();
    }

    public function info()
    {


        if (request()->isAjax()) {
            $data = input('');
            $res = Db::name('goods')->where('id', $data['id'])->update($data);
            if ($res === false) {
                $this->success('操作失败');
            }
            $this->success('操作成功');

        } else {

            $res = Db::name('admin')->find();
            $this->assign('item', $res);
            return $this->fetch();
        }

    }


    /**
     * 网站配置
     * @return mixed
     */
    public function index()
    {
        if (request()->isAjax()) {
            $data = input();
            $data = json_encode($data);
            $op = Db::name('option');
            $res = $op->where('option_name', 'web_set')->find();
            if ($res) {
                $op->where('option_name', 'web_set')->update(['option_value' => $data]);
            } else {
                $op->insertGetId(['option_name' => 'web_set', 'option_value' => $data]);
            }
            $this->success('操作成功');
        } else {

            $op = Db::name('option');
            $res = $op->where('option_name', 'web_set')->value('option_value');
            $this->assign('item', json_decode($res, true));

            return $this->fetch();
        }

    }


    //七牛配置
    public function oss()
    {

        if (request()->isAjax()) {
            $data = input();
            $data = json_encode($data);
            $op = Db::name('option');
            $res = $op->where('option_name', 'qiniu')->find();
            if ($res) {
                $op->where('option_name', 'qiniu')->update(['option_value' => $data]);
            } else {
                $op->insertGetId(['option_name' => 'qiniu', 'option_value' => $data]);
            }
            $this->success('操作成功');
        } else {

            $op = Db::name('option');
            $res = $op->where('option_name', 'qiniu')->value('option_value');
            $this->assign('item', json_decode($res, true));

            return $this->fetch();
        }


    }


    // 微信配置
    public function wxconfig()
    {
        if (request()->isAjax()) {
            $data = input();
            $data = json_encode($data);
            $op = Db::name('option');
            $res = $op->where('option_name', 'wx')->find();
            if ($res) {
                $op->where('option_name', 'wx')->update(['option_value' => $data]);
            } else {
                $op->insertGetId(['option_name' => 'wx', 'option_value' => $data]);
            }
            //清除access_token jstick
            Db::name('wxconfig')
                ->where('id', '>', 0)
                ->update(['value' => '', 'extend' => '']);
            $this->success('操作成功');
        } else {

            $op = Db::name('option');
            $res = $op->where('option_name', 'wx')->value('option_value');
            $this->assign('item', json_decode($res, true));

            $wx_temp = cmf_get_option('wx_temp');

            $this->assign('temp', $wx_temp);


            return $this->fetch();
        }
    }


    // 模板消息
    public function set_temp()
    {
        if (request()->isAjax()) {
            $data = input();
            $data = json_encode($data);
            $op = Db::name('option');
            $res = $op->where('option_name', 'wx_temp')->find();
            if ($res) {
                $op->where('option_name', 'wx_temp')->update(['option_value' => $data]);
            } else {
                $op->insertGetId(['option_name' => 'wx_temp', 'option_value' => $data]);
            }
            $this->success('操作成功');
        }
    }


    /**
     * 消息测试
     */
    public function temp_test()
    {
        $temp_id = input('temp_id');
        $openid = input('openid');
        $weixin = new \app\index\controller\Weixin();


        $back_url = url("home/my/my", ['order_id' => 1111]);
        $res = false;
        if ($temp_id == 'order') {
            $res = $weixin->order_msg($openid, $back_url, rand(1000000, 9999999), '胡金飞', '七一路', '6月1日');
        } elseif ($temp_id == 'jingbiao') {
            $res = $weixin->jingbiao_msg($openid, $back_url, rand(1000000, 9999999), '胡金飞', '七一路', '6月1日');
        } elseif ($temp_id == 'yanshou') {
            $res = $res = $weixin->yanshou_msg($openid, $back_url, rand(1000000, 9999999) . '七一路东段', '胡金飞', '13137683557');
        } elseif ($temp_id == 'dakuan') {
            $res = $res = $weixin->dakuan_msg($openid, $back_url, '胡金飞', '中国银行', '600元', '6月1日 13:45');
        }
        if ($res) {
            $this->success('发送成功');
        }
        $this->error('发送失败');


    }


    /**
     * 短信配置
     * @return mixed
     */
    public function sms()
    {
        if (request()->isAjax()) {
            $data = input();
            $data = json_encode($data);
            $op = Db::name('option');
            $res = $op->where('option_name', 'sms')->find();
            if ($res) {
                $op->where('option_name', 'sms')->update(['option_value' => $data]);
            } else {
                $op->insertGetId(['option_name' => 'sms', 'option_value' => $data]);
            }
            $this->success('操作成功');
        } else {


            $op = Db::name('option');
            $res = $op->where('option_name', 'sms')->value('option_value');
            $this->assign('item', json_decode($res, true));

            return $this->fetch();
        }
    }


    //城市管理
    public function city()
    {

        $city = Db::name('addcity');
        $res1 = $city
            ->where('type', 3)
            ->order('id', 'desc')
            ->select();

        if (request()->isAjax()) {
            $city = Db::name('addcity');
            $res1 = $city
                ->where('type', 3)
                ->order('id', 'desc')
                ->select();
            $res = [];
            foreach ($res1 as $index => $re) {

                $shi = $city
                    ->where('id', $re['pid'])
                    ->find();
                $sheng = $city
                    ->where('id', $shi['pid'])
                    ->find();
                $res[$index] = ["sheng" => $sheng['name'], "shi" => $shi['name'], 'xian' => $re['name']];
            }
            return json(['code' => 1, 'msg' => 'ok', 'data' => $res]);
        }

        $this->assign('count', count($res1));
        return $this->fetch();
    }

    //添加城市
    public function cityadd()
    {
        if ($this->request->isAjax()) {
            $data = input();
            $city = Db::name('addcity');
            if ($data['sheng']) {
                $res = $city
                    ->where('name', $data['sheng'])
                    ->find();
                if (!$res) {
                    $sid = $city
                        ->insertGetId(["pid" => 0, "name" => $data['sheng'], "type" => 1]);
                } else {
                    $sid = $res['id'];
                }
                $res2 = $city
                    ->where('name', $data['shi'])
                    ->where('pid', $sid)
                    ->find();
                if (!$res2) {
                    $shiid = $city
                        ->insertGetId(["pid" => $sid, "name" => $data['shi'], "type" => 2]);
                } else {
                    $shiid = $res2['id'];
                }
                //dump($shiid).die();
                $res3 = $city
                    ->where('name', $data['xian'])
                    ->where('pid', $shiid)
                    ->find();
                if (!$res3) {
                    $xian = $city
                        ->insertGetId(["pid" => $shiid, "name" => $data['xian'], "type" => 3]);
                } else {
                    $xian = $res3['id'];
                }
                $this->success('添加成功');
            } else {
                $this->error('添加失败');
            }


        }


        return $this->fetch();
    }

    //删除操作
    public function del()
    {
        $data = input('');
        $ids = $data['ids'];
        $city = Db::name('addcity');
        foreach ($ids as $id) {

            $xian = $city->where('name', $id)->find();
            $res = $city->where('name', $id)->delete();
            //通过县查找到对应市的id
            $shiid = $xian['pid'];
            //查找该是的数据
            $shidata = $city->where('id', $shiid)->find();
            //查找它的下面是否有对应的县
            $shi = $city->where('pid', $shiid)->select();
            //如果没有就删除该市
            if ($shi == null) {
                $res1 = $city->where('id', $shiid)->delete();
            }
            //通过市查找到对应省的id
            $shengid = $shidata['pid'];
//             var_dump($shengid);
            //查找它的下面是否有对应的市
            $sheng = $city->where('pid', $shengid)->select();
            //如果没有就删除该省
            if ($sheng == null) {
                $res2 = $city->where('id', $shengid)->delete();
            }
        }
        if ($res) {
            $this->success('操作成功');
        }
    }


    //城市管理中的  搜索功能
    public function base_so()
    {
        $status[0] = '禁用';
        $status[1] = '正常';
        $page = input('page') ? input('page') : 1;
        $limit = input('limit') ? input('limit') : 10;
        $key = trim(input('keyword'));
        if (!$key) {
            $this->error('没有内容');
        }

        $res = Db::name('addcity')->page($page, $limit)
            //搜索相关
            //参数绑定
            // ->where('id > :id AND name LIKE :name ', ['id' => 0, 'name' => 'thinkphp%'])
            ->where('name LIKE :key ', ['key' => "%$key%"])
            ->select();

        //根据关键字返回 相应数据
        $total =
            Db::name('yuyue')
                ->where(' name LIKE :key ', ['key' => "%$key%"])
                ->count();
        return json(['code' => 1, 'msg' => 'ok', 'total' => $total, 'data' => $res]);
    }


    //城市选择器
    public function selectcity()
    {
        if ($this->request->isAjax()) {
            $data = input();
            $sid = $data['id'];
            $city = Db::name('addcity');
            $res = $city
                ->where('type', 2)
                ->where('pid', $sid)
                ->select();
            return json(['code' => 1, 'msg' => 'ok', 'data' => $res]);
        }

    }

    public function selectxian()
    {
        if ($this->request->isAjax()) {
            $data = input();
            $sid = $data['id'];
            $city = Db::name('addcity');
            $res = $city
                ->where('type', 3)
                ->where('pid', $sid)
                ->select();
            return json(['code' => 1, 'msg' => 'ok', 'data' => $res]);
        }

    }

    public function get_biz_city($sheng_id, $shi_id)
    {

        $city = Db::name('addcity');
        $sheng = $city->where('type', 1)->select();


        $shi = $city->where('pid', $sheng_id)
            ->where('type', 2)
            ->select();


        $xian = $city->where('pid', $shi_id)
            ->where('type', 3)
            ->select();

        return ['sheng' => $sheng, 'shi' => $shi, 'xian' => $xian];


    }


}
